package cn.xyh.constant;

import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SysUtil {
    private static Logger log = Logger.getLogger(SysUtil.class);

    public static String captureName(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    /**
     * @param text
     * @return
     */
    public Date str2Date(String text) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date date = simpleDateFormat.parse(text);
            return date;
        } catch (Exception px) {
            px.printStackTrace();
        }
        return null;
    }

    /**
     * �����
     *
     * @return
     */
    public String date2Str(Date date) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        return f.format(date);

    }



    /**
     * ���򷵻�false
     *
     * @param obj
     * @return
     */
    public static boolean isNull(Object obj) {
        if (obj == null || "null".equalsIgnoreCase(String.valueOf(obj))) return true;
        else {
            if ("".equalsIgnoreCase(String.valueOf(obj).trim())) {
                return true;
            }
        }
        return false;
    }

    /**
     * ���򷵻�false
     *
     * @param obj
     * @return
     */
    public static boolean notNull(Object obj) {
        if (obj == null) return false;
        else {
            if ("".equalsIgnoreCase(String.valueOf(obj).trim())) {
                return false;
            } else if ("null".equalsIgnoreCase(String.valueOf(obj))) {
                return false;
            }
        }
        return true;
    }


    /**
     * ��request�л�ȡֵ
     *
     * @param request
     * @param key
     * @return 9:25:08 PM
     * String
     */
    public static String getRequestNotNullValue(HttpServletRequest request, String key) {
        String restr = "";
        restr = request.getParameter(key) == null ? "" : String.valueOf(request.getParameter(key));
        return restr;
    }

    /**
     * ���
     *
     * @param obj
     * @return 2:22:32 PM
     * String
     */
    public static String replaceNullToSpace(Object obj) {
        String restr = "";
        restr = obj == null ? "" : String.valueOf(obj).trim();
        return restr;
    }

    /**
     * ���кŸ�ʽ����
     *
     * @param date
     * @return
     */
    public static String formatDouble(double f, String formatStr) {
        DecimalFormat format = new DecimalFormat(formatStr);
        //format.setRoundingMode(RoundingMode.DOWN);
        return format.format(f);
    }

    public static String getFixes(String str) {
        String restr = "";
        String[] fixes = str.split(",");
        log.debug("Fixes Length:" + fixes.length);
        for (int i = 0; i < fixes.length; i++) {
            if ("".equals(restr)) restr += "?";
            else restr += ",?";
        }

        return restr;
    }

    public static String getUpdateStm(String str) {
        String restr = "";
        String[] fixes = str.split(",");
        log.debug("UpdateStm Length:" + fixes.length);
        for (String f : fixes) {
            if ("".equals(restr)) restr += f + "=?";
            else restr += "," + f + "=?";
        }

        return restr;
    }

    /**
     * 判断字符串 是否存在 字符串数组中
     *
     * @param str
     * @param x
     * @return
     */
    public static boolean strInArray(String str, String strs[]) {
        boolean exist = false;
        for (int i = 0; i < strs.length; i++) {
            if (str.equals(strs[i]))
                exist = true;
            break;
        }
        return exist;
    }


    /**
     * NK0315-1046XL
     * NK0315 品名
     * 尺码 XL
     * 颜色1046
     * 9         2  >1
     * NK0315-10 46 XL
     *
     * @param code
     * @return
     */
    public static String[] deCodeSaleCode1(String code) {
        if (code.length() < 12) {
            return null;
        } else {
            String[] codes = new String[3];
            int i0 = 0;
            int i1 = 6;
            int i2 = 11;
            int len = code.length();
            codes[0] = code.substring(i0, i1);
            codes[1] = code.substring(i1 + 1, i2);
            codes[2] = code.substring(i2, len);
            return codes;
        }
    }

    /**
     * 货号- 颜色-尺码
     * F044-02-26*48cm
     * 按照标准格式 来 A-B-C
     *
     * @param code
     * @return
     */
    public static String[] deCodeSaleCode(String code) {
        if (SysUtil.isNull(code) || "undefined".equalsIgnoreCase(code)) return null;
        System.out.println("sale code:" + code);
        String[] codes = new String[3];

        int last = code.lastIndexOf("-");
        codes[2] = code.substring(last + 1, code.length());   //从后面来；
        String sub_code = code.substring(0, last);

        int center = sub_code.lastIndexOf("-");
        codes[1] = sub_code.substring(center + 1, sub_code.length());

        codes[0] = sub_code.substring(0, center);

        return codes;
    }

    public static int String2int(String s) {
        int i = 0;
        if (SysUtil.isNull(s)) {
            return i;
        } else {
            s = s.replaceAll(",", "");
        }
        try {
            i = Integer.parseInt(s);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            log.debug("异常数字：" + i);

        }
        return i;
    }

    public static float String2float(String s) {
        float f = 0;
        if (SysUtil.isNull(s)) {
            return f;
        } else {
            s = s.replaceAll(",", "");
        }
        try {
            f = Float.parseFloat(s);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            log.debug("异常数字：" + f);

        }
        return f;
    }

    public static double String2double(String s) {
        double d = 0;
        if (SysUtil.isNull(s)) {
            return d;
        } else {
            s = s.replaceAll(",", "");
        }
        try {
            d = Double.parseDouble(s);
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            log.debug("异常数字：" + d);
        }
        return d;
    }

    public static void main(String[] args) {
//		String[] a = new String[] {"123","345","34567"};
//		String b = "1234";
//		boolean exist = SysUtil.strInArray(b,a);
////		System.out.println(now());
//		String[] codes = deCodeSaleCode("NK0317-05-2XL");
//		System.out.println(codes[0]);
//		System.out.println(codes[1]);
//		System.out.println(codes[2]);

//		double f = 23.4;
//		DecimalFormat format = new DecimalFormat("#");
//		format.setRoundingMode(RoundingMode.DOWN);
//		System.out.println(format.format(f));


        SysUtil.String2double("100,000".replaceAll(",", ""));


    }

}


